/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

#include "scenic_jni_RenderCanvas.h"
#include "jawt_md.h"

#include <assert.h>

#include "common.h"
#include "Debug.h"


using namespace Scenic::Renderer;


#ifdef DIRECTX
static LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    return DefWindowProc( hWnd, msg, wParam, lParam );
}

void createDummyDevice()
{
	printf("Creating a dummy device for offscreen rendering.\n");
	
	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
		GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
		"DummyWindow", NULL };

	RegisterClassEx( &wc );

	HWND hWnd = CreateWindow( "DummyWindow", "DummyWindow", 
			                      WS_OVERLAPPEDWINDOW, 100, 100, 900, 900,
				                  GetDesktopWindow(), NULL, wc.hInstance, NULL );
	Device *device = new Device(hWnd);
}
#else
void createDummyDevice()
{
	printf("Dummy devices are not available on Linux!\n");
}
#endif

JNIEXPORT void JNICALL Java_scenic_jni_RenderCanvas_initLibrary
  (JNIEnv *, jclass)
{
	ENTER(Java_scenic_jni_RenderCanvas_initLibrary);
	createDummyDevice();
}

JNIEXPORT jint JNICALL Java_scenic_jni_RenderCanvas_nativeCreate(
	JNIEnv* env, jobject canvas)
{
	Synchronized sync(renderingLock);

	Canvas *nativeCanvas = new Canvas(env, canvas);

	return nativeCanvas->id;
}

JNIEXPORT void JNICALL Java_scenic_jni_RenderCanvas_nativeReset(
	JNIEnv* env, jobject canvas, jint id)
{
	Synchronized sync(renderingLock);

	ENTER(java_scenic_jni_RenderCanvas_nativeReset);

	Canvas::get(id)->reset();
}

JNIEXPORT void JNICALL Java_scenic_jni_RenderCanvas_nativeRelease(
	JNIEnv* env, jobject canvas, jint id)
{
	Synchronized sync(renderingLock);

	ENTER(java_scenic_jni_RenderCanvas_nativeReset);

	delete Canvas::get(id);
}
